##
# File.pm
# uAPI Public License
# @change delete is keyword in C(++) method renamed to del
# @author Bastie
##
package uapi::io::File;

use 5; #Perl 5 or higher
use strict;

  ##
  # Constructor
  ##
  sub new () {
    my $type = shift;   # save class name
    my $this = {};      # create hash - array are faster but this come later
    $this->{path} = $_[0];

    bless $this, $type; # create object instance
    return $this;       # return object instance
  }

  ##
  # Destructor
  ##
  sub DESTROY () {
  }

  sub exist () {
    my $this = shift;
    return -e $this->{path};
  }

  sub isDirectory () {
    my $this = shift;
    return -d $this->{path};
  }

  sub isFile () {
    my $this = shift;
    return -f $this->{path};
  }

  sub makeDirectory () {
    my $this = shift;
    return mkdir ($this->{path},0777);
  }

  sub listFiles () {
    my $this = shift;
    my @sub;
    my $file;
    opendir(DIR, $this->{path}) || return @sub;
    while ($file = readdir(DIR)) {
      my $fileInstance = new uapi::io::File ($file);
      push (@sub, $fileInstance); # add to end
    }
    return @sub;
  }

  sub del () {
    my $ok = undef;
    my $this = shift;
    if ($this->isDirectory()) {
      $ok = rmdir ($this->{path});
    }
    else {
      $ok = unlink ($this->{path});
    }
    return $ok;
  }

  sub rename () {
    my $this = shift;
    my $ok = undef;
    my $newName = $_[0];
    $ok = rename ($this->{path}, $newName);
    if ($ok) {
      $this->{path} = $newName;
    }
    return $ok;
  }

  sub toString () {
    my $this = shift;
    return $this->{path};
  }

1;

__END__

=pod

=head1 NAME

File.pm - uAPI file implementation

=head1 DESCRIPTION

=head1 AUTHOR

Bastie - Sebastian Ritter